#include "esp8266.h"
#include "main.h"
#include "usart.h"
#include "lcd.h"
#include "key_led.h"
#include "fifo.h"
#include "file_opera.h"
#include <stdio.h>
#include <string.h>

#ifdef user_FreeRTOS

#include "FreeRTOS.h"
#include "semphr.h"
#include "queue.h"
#include "cmsis_os.h"

extern osThreadId_t Task_SD_WriteHandle;
extern osMessageQueueId_t Wifi_rawdataHandle;
extern osSemaphoreId_t BinSem_WifiGetHandle;
extern osSemaphoreId_t CountSem_keyTimesHandle;
extern osSemaphoreId_t BinSem_usart3_getHandle;
extern osMessageQueueId_t Queue_FileSizeTotalHandle;
extern osMessageQueueId_t Queue_FileTypeWritingHandle;
extern osSemaphoreId_t BinSem_usart3_getHandle;
extern UART_HandleTypeDef huart3;
extern uint8_t* current_rx_buffer;

/**
  * 需要使用ESP8266发送一些信息都在该线程中执行，具有以下功能
  * 1、初始化ESP8266模块
  * 2、通过AppTask_SD_Write发来的信息，向上位机发送传输停止/出错信息
  */
void AppTask_ESP8266(void *argument)
{
	uint8_t pData[50] = "AT+CIPMUX=1\r\n";
	uint8_t error_str[50] = {0};
	uint32_t file_currentSize = 0;
	filetypes file_type_writing;
	extern fifo_s_t uart_rx_fifo;
	//初始化ESP8266模块
	HAL_NVIC_DisableIRQ(USART3_IRQn);
	HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
	osDelay(pdMS_TO_TICKS(100));
	sprintf((char*) pData, "AT+CIPSERVER=1,8888\r\n");
	HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
	osDelay(pdMS_TO_TICKS(100));
	HAL_NVIC_EnableIRQ(USART3_IRQn);

	for (;;)
	{
		uint32_t EnterClear = 0x00000000;
		uint32_t ExitClear = 0xFFFFFFFF;
		if(xTaskNotifyWait(EnterClear, ExitClear, &file_currentSize, portMAX_DELAY) == pdPASS)
		{
			xQueuePeek(Queue_FileTypeWritingHandle, &file_type_writing, portMAX_DELAY);
			//文件已传输完成，需发送File Receive OK至上位机
			if((file_type_writing == file_wav_full) || (file_type_writing == file_wav_end))
			{	//文件传输完成，向上位机发送完成标志
				HAL_NVIC_DisableIRQ(USART3_IRQn);
				//使用串口阻塞发送时需先屏蔽接收中断，否则容易导致后续中断接收无法触发的问题！！！
				sprintf((char*)pData, "AT+CIPSEND=0,17\r\n");
				HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
				osDelay(pdMS_TO_TICKS(100));
				sprintf((char*)pData, "File Receive OK\r\n");
				HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
				osDelay(pdMS_TO_TICKS(100));

				HAL_NVIC_EnableIRQ(USART3_IRQn);
				//清空串口线程至写SD任务线程的数据缓冲区
				uint16_t fifo_len0 = 0;
				fifo_len0 = fifo_s_used(&uart_rx_fifo);
				fifo_s_discard(&uart_rx_fifo, fifo_len0);
			}
			else if(file_type_writing == file_wav_break)
			{	//文件中途出错，重新连接TCP清空缓存
				vTaskSuspend(Task_SD_WriteHandle);		//挂起写SD卡任务
				HAL_NVIC_DisableIRQ(USART3_IRQn);

				sprintf((char*)error_str, "Error size:%ld\r\n", file_currentSize);
				sprintf((char*)pData, "AT+CIPSEND=0,%d\r\n", strlen((char*)error_str));
				HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
				osDelay(pdMS_TO_TICKS(100));
				HAL_UART_Transmit(&huart3, error_str, strlen((char*) error_str), 100);
				osDelay(pdMS_TO_TICKS(100));
				sprintf((char*)pData, "AT+CIPCLOSE=0\r\n");
				HAL_UART_Transmit(&huart3, pData, strlen((char*) pData), 100);
				osDelay(pdMS_TO_TICKS(200));

				HAL_NVIC_EnableIRQ(USART3_IRQn);
				//清空串口线程至写SD任务线程的数据缓冲区
				uint16_t fifo_len0 = 0;
				fifo_len0 = fifo_s_used(&uart_rx_fifo);
				fifo_s_discard(&uart_rx_fifo, fifo_len0);
				xSemaphoreTake(BinSem_usart3_getHandle, portMAX_DELAY);
				vTaskResume(Task_SD_WriteHandle);		//恢复写SD卡任务
			}
		}
		osDelay(100);
	}
	/* USER CODE END AppTask_ATK8266 */
}

#else

void myESP8266_Init(void)
{
	uint8_t pData[100]="AT+CIPMUX=1\r\n";
	HAL_UART_Transmit(&huart3, pData, sizeof(pData),100);
	HAL_Delay(100);
	sprintf((char *)pData,"AT+CIPSERVER=1,8888\r\n");
	HAL_UART_Transmit(&huart3, pData, sizeof(pData),100);
	HAL_Delay(100);
}
#endif

//while(1)
//{
//	if(xSemaphoreTake(BinSem_usart3_getHandle, portMAX_DELAY) == pdTRUE)
//	{	//当接收到"File name:"后说明接下来传输的就是需要的数据
//		uint16_t fifo_len = 0;
//		fifo_len = fifo_s_used(&uart_rx_fifo);
//		if(fifo_len > 100)
//		{
//			fifo_s_discard(&uart_rx_fifo, fifo_len - 100);
//			fifo_len = 100;
//		}
//		fifo_s_gets(&uart_rx_fifo, (char *)usart3_rawdata, fifo_len);
//		if(strstr((char *)usart3_rawdata, str_handle) != NULL)
//		{	//使写SD卡任务解除挂起状态，准备开始写入数据
//			file_type_writing = file_wav;
//			xQueueOverwrite(Queue_FileTypeWritingHandle, &file_type_writing);
//			xSemaphoreTake(BinSem_usart3_getHandle, 0);
//			vTaskResume(Task_SD_WriteHandle);
//			break;
//		}
//	}
//}
